
package com.skcc.cop.mrtaxi4driver.biz;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.TelephonyManager;

import com.google.android.gcm.GCMRegistrar;
import com.projectteam.http.util.DeviceUtil;
import com.skcc.cop.mrtaxi4driver.BaseActivity;
import com.skcc.cop.mrtaxi4driver.R;
import com.skcc.cop.mrtaxi4driver.http.RegisterPushIdListener;
import com.skcc.cop.mrtaxi4driver.util.Dialogs;
import com.skcc.cop.mrtaxi4driver.util.Dialogs.DialogListener;
import com.skcc.cop.mrtaxi4driver.util.GCMUtils;
import com.skcc.cop.mrtaxi4driver.util.Utils;
import com.skcc.cop.mrtaxi4driver.util.Variables;


public class IntroActivity extends BaseActivity {
	
	// ===========================================================
    // Constants
    // ===========================================================
    private static final String TAG = IntroActivity.class.getSimpleName();

    
    // ===========================================================
    // Fields
    // ===========================================================
    
    
    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// Make sure the device has the proper dependencies.
		GCMRegistrar.checkDevice(this);
		// Make sure the manifest was properly set - comment out this line
		// while developing the app, then uncomment it when it's ready.
		GCMRegistrar.checkManifest(this);
		
		setContentView(R.layout.activity_intro);
		
		registerReceiver(registrationIdReceiver, 
				new IntentFilter(GCMUtils.ON_REGISTRATIONID_RECEIVED_ACTION));
		
		if (checkDeviceValidation()) {
			final String regId = GCMRegistrar.getRegistrationId(this);
	    	if (regId.equals("")) {
	    		showProgressDialog(this, R.string.loading);
	    		
	            // Automatically registers application on startup.
	            GCMRegistrar.register(this, GCMUtils.SENDER_ID);
	        } else {
	        	// Device is already registered on GCM, check server.
	        	if (GCMRegistrar.isRegisteredOnServer(this)) {
	        		showUserAgreementNotice();
	            } else {
	            	if (!Utils.checkNetworkStatus(IntroActivity.this)) {
	     				Dialogs.showDialog(IntroActivity.this, getString(R.string.network_is_not_available), dialogListener); 
	     			} else {
	     				registerOnServer(regId);
	     			}
	            }
	        }
		} 
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		unregisterReceiver(registrationIdReceiver);
	    GCMRegistrar.onDestroy(getApplicationContext());
		
		super.onDestroy();
	}
	
	
	// ===========================================================
    // Methods
    // ===========================================================
	private void registerOnServer(String regId) {
		showProgressDialog(this, R.string.loading);
		
		gateway.registerPushId(regId, new RegisterPushIdListener() {

			@Override
			public void onNoNetwork() {
				dismissProgressDialog();
				
				Dialogs.showDialog(IntroActivity.this, getString(R.string.no_network), dialogListener);
			}

			@Override
			public void onNoResponse() {
				dismissProgressDialog();
				
				Dialogs.showDialog(IntroActivity.this, getString(R.string.no_respond), dialogListener);
			}

			@Override
			public void onSessionTimeOut() {
				dismissProgressDialog();
				
				Dialogs.showDialog(IntroActivity.this, getString(R.string.session_timeout), dialogListener);
			}

			@Override
			public void onSuccess() {
				dismissProgressDialog();
				
				GCMRegistrar.setRegisteredOnServer(IntroActivity.this, true);
				
				showUserAgreementNotice();
			}

			@Override
			public void onFail(int code, String msg) {
				dismissProgressDialog();
				
				Dialogs.showDialog(IntroActivity.this, getString(R.string.server_message) + msg, dialogListener);
			}
		});
	}
	
	/**
	 * 사용자 동의 여부을 체크한다.
	 */
	private void showUserAgreementNotice() {
		if (Variables.BOOLEAN_VALUE_Y.equals(application.getPreferenceManager().getString(Variables.USER_AGREEMENT, ""))) {
			Intent intent = new Intent(IntroActivity.this, HomeActivity.class);
			startActivity(intent);
			finish();
		} else {
			Dialogs.showDialogYesNo(IntroActivity.this, getString(R.string.user_agreement), dialogListener2); 
		}
	}
	
	private boolean checkDeviceValidation() {
		TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		if (tm.getSimState() == TelephonyManager.SIM_STATE_ABSENT) {
			//Dialogs.showDialog(IntroActivity.this, getString(R.string.sim_state_absent), dialogListener);
			//return false;
		}
		
		DeviceUtil device = new DeviceUtil(this);
		if ("".equals(device.getMDN())) {
			//Dialogs.showDialog(IntroActivity.this, getString(R.string.msisdn_value_null), dialogListener);
			//return false;
		}
		
		if (!Utils.checkNetworkStatus(this)) {
			Dialogs.showDialog(IntroActivity.this, getString(R.string.network_is_not_available), dialogListener);
			return false;
		}
		
		return true;
	}
	
	
	// ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
	private final BroadcastReceiver registrationIdReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
        	dismissProgressDialog();
        	
            String regId = intent.getExtras().getString(GCMUtils.REGISTRATION_ID);

            if (!Utils.checkNetworkStatus(IntroActivity.this)) {
				Dialogs.showDialog(IntroActivity.this, getString(R.string.network_is_not_available), dialogListener); 
			} else {
				registerOnServer(regId);
			}
        }
    };
	
	private DialogListener dialogListener = new DialogListener() {

		@Override
		public void onClickedOk() {
			Dialogs.dismiss();
			
			finish();
		}

		@Override
		public void onClickedCancel() {
			// TODO Auto-generated method stub
		}
	};
	
	private DialogListener dialogListener2 = new DialogListener() {
		
		@Override
		public void onClickedOk() {
			
			application.getPreferenceManager().putString(Variables.USER_AGREEMENT, Variables.BOOLEAN_VALUE_Y);
			
			Intent intent = new Intent(IntroActivity.this, HomeActivity.class);
			startActivity(intent);
			finish();
		}

		@Override
		public void onClickedCancel() {
			finish();
		}
	};
	
}
